package com.yuanwushe.blue.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.yuanwushe.blue.models.Fruit;
import com.yuanwushe.blue.onceandroid.R;

import java.util.List;

/**
 * Created by blue on 2018/5/28.
 */

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {

    List<Fruit> mFruitList;
    public FruitAdapter(List<Fruit> fruitList){
        mFruitList = fruitList;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Fruit fruit = mFruitList.get(position);
        holder.NameText.setText(fruit.getFruitName());
        holder.DisText.setText(fruit.getFruitDis());
        holder.View.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext(), fruit.getFruitName(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mFruitList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView NameText;
        TextView DisText;
        View View;
        public ViewHolder(View itemView) {
            super(itemView);
            NameText = itemView.findViewById(R.id.fruit_name);
            DisText = itemView.findViewById(R.id.fruit_dis);
            View = itemView;
        }
    }
}
